package com.sms.demo.Controller;
/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2022/03/11/17:35
 * @Description:
 */

import com.sms.demo.Service.UserService;
import com.sms.demo.entity.User;
import com.sms.demo.utils.RedisUtil;
import com.sms.demo.utils.SendSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 *@ClassName SendSmsController
 *@Description
 *@Author 王琛
 *@Date 2022/3/11 17:35
 @Version 1.0
 */
@RestController
@RequestMapping
public class SendMsgController {

    @Autowired
    private UserService userService;

    @Autowired
    private SendSmsUtil sendSmsUtil;

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 发送验证码
     * @author 华达州
     * @date 2021/8/6 21:35
     * @param phone
     * @return java.lang.String
     */
    @PostMapping("sendCode")
    @ResponseBody
    public String sendCode(@RequestParam String phone){
        String s = sendSmsUtil.sendSms(phone);
        return sendSmsUtil.sendSms(phone);
    }


    /**
     * 使用手机号验证码登录
     * @author 华达州
     * @date 2021/8/6 20:26
     * @return java.lang.String
     */
    @PostMapping("codeLogin")  //腾讯云API推荐使用post请求
    @ResponseBody
    public String codeLogin(@RequestParam("phone") String phone,@RequestParam("code")String code){
        User user = userService.getOne(userService.lambdaQuery().eq(User::getPhoneNum,phone));
        if(user==null){
            return "用户不存在";
        }
        String oldCode = (String) redisUtil.get(phone);
        if(!code.equals(oldCode)){
            return "验证码错误";
        }
        return "登录成功";
    }
}
